package algorithm_learn.dataStructure;

public class Code_LinkedListToQueueAndStack {
    public static class Node<V>{
        public V value;
        public Node<V> next;

        public Node(V value) {
            this.value = value;
        }
    }
    /***
     * 自己实现的队列
     * @param <V> 节点数据类型
     */
    public static class MyQueue<V>{
        private Node<V> head;
        private Node<V> tail;
        private int size;

        public MyQueue(Node<V> head, Node<V> tail, int size) {
            this.head = head;
            this.tail = tail;
            this.size = size;
        }
        public MyQueue() {
            this.head = null;
            this.tail = null;
            this.size = 0;
        }
        public boolean isEmpty(){
            return size == 0;
        }
        public int getSize(){
            return size;
        }

        /***
         * 加入节点元素
         * @param value
         */
        public void offer(V value){
            Node<V> cur = new Node<V>(value);
            if(tail == null){
                head = cur;
                tail = cur;
            }else {
                tail.next = cur;
                tail = cur;
            }
            size++;
        }
        public V poll(){
            V ans = null;
            if(head != null){
                ans = head.value;
                head = head.next;
                size--;
            }
            //如果没有数据了，就表示 尾部已经超过头部了 不行！
            if(head == null){
                tail = null;
            }
            return ans;
        }

    }

    /***
     * 自己实现的栈结构
     * @param <V>
     */
    public static class MyStack<V>{
        private Node<V> head;
        private int size ;
        public MyStack(){
            this.head = null;
            this.size = 0;
        }

        /***
         * 入栈
         * @param value 入栈的值
         */
        public void push(V value){
            Node<V> cur = new Node<>(value);
            if(head == null){
                head = cur;
            }else {
                cur.next = head;
                head = cur;
            }
            size++;
        }

        /***
         * 出栈
         * @return
         */
        public V pop(){
            V ans = null;
            if(head != null){
                ans = head.value;
                head = head.next;
                size --;
            }
            return ans;
        }

        /***
         * 返回顶峰元素
         * @return
         */
        public V peek(){
            return head != null ? head.value : null;
        }

    }
    public static void main(String[] args) {

    }
}
